﻿using Hx.ADSyncPlatform.Db;
using Hx.ADSyncPlatform.Entity.Entities;
using Hx.ADSyncPlatform.Infrastructure.Expression.Common;
using Hx.ADSyncPlatform.Infrastructure.Page;
using Hx.ADSyncPlatform.Service.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Hx.ADSyncPlatform.Service
{
    public class InterfaceLogPasswordService : BaseService<Interface_PasswordLogs>
    {
        public InterfaceLogPasswordService(DbContext dbContext) : base(dbContext)
        {

        }
        /// <summary>
        /// 修改密码操作日志
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public async Task<PageResult<Interface_PasswordLogs>> GetPageList(Dictionary<string, string> param, Pagination pagination)
        {

            RefAsync<int> totalCount = 0;

            Expression<Func<Interface_PasswordLogs, bool>> expression = n => true;

            if (param.ContainsKey("minDate"))
            {
                expression = expression.And(n => n.sync_createTime >= Convert.ToDateTime(param["minDate"]));
            }
            if (param.ContainsKey("maxDate"))
            {
                expression = expression.And(n => n.sync_createTime < Convert.ToDateTime(param["maxDate"]).AddDays(1));
            }
            if (param.ContainsKey("requestId"))
            {
                expression = expression.And(n => n.requestId.Contains(param["requestId"]));
            }
            if (param.ContainsKey("userId"))
            {
                expression = expression.And(n => n.userId.Equals(param["userId"]));
            }
            if (param.ContainsKey("sync_isSuccess"))
            {
                expression = expression.And(n => n.sync_isSuccess.Equals(param["sync_isSuccess"]));
            }
            var res = await base.dbClient.Queryable<Interface_PasswordLogs>()
                .Where(expression)
                 .OrderBy(n => n.sync_createTime, OrderByType.Desc)
                 .ToPageListAsync(pagination.pageNum, pagination.pageSize, totalCount);


            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pagination.pageSize.ObjToDecimal())).ObjToInt();
            pagination.pageCount = pageCount;
            pagination.dataCount = totalCount.Value;

            return new PageResult<Interface_PasswordLogs>(data: res, pagination: pagination);
        }
    }
}
